package com.dq.educms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.commonutils.vo.Result;
import com.dq.educms.entity.CrmBanner;
import com.dq.educms.service.CrmBannerService;
import com.dq.educms.vo.BannerVo;
import com.dq.educms.vo.param.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 前台接口
 * CrmBanner-API
 * @author Dq
 * @since 2022-08-04 17:56:09
 */
@RestController
@RequestMapping("educms/banner")
// @CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询banner
     * @param pageParam
     * @return
     */
    @PostMapping("pageBanner")
    public Result getBannerList(@RequestBody PageParam pageParam){
        Page<CrmBanner> page = new Page<CrmBanner>(pageParam.getCurrentPage(), pageParam.getPageSize());
        Page<CrmBanner> bannerPage = bannerService.page(page,null);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("bannerItems",bannerPage.getRecords());
        resultMap.put("total",bannerPage.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 根据id获取Banner
     * @param bannerId
     * @return
     */
    @GetMapping("getBanner/{id}")
    public Result getBannerById(@PathVariable("id") String bannerId){
        CrmBanner banner = bannerService.getById(bannerId);
        BannerVo bannerVo = new BannerVo();
        BeanUtils.copyProperties(banner,bannerVo);
        HashMap<String, BannerVo> map = new HashMap<>();
        map.put("banner",bannerVo);
        return Result.success(map);
    }

    /**
     * 添加banner图片
     * @param bannerVo
     * @return
     */
    @PostMapping("addBanner")
    public Result addBanner(@RequestBody BannerVo bannerVo){
        CrmBanner crmBanner = new CrmBanner();
        BeanUtils.copyProperties(bannerVo,crmBanner);
        bannerService.save(crmBanner);
        return Result.success("添加成功");
    }

    /**
     * 修改banner
     * @param bannerVo
     * @return
     */
    @PostMapping("updateBanner")
    public Result updateBanner(@RequestBody BannerVo bannerVo){
        CrmBanner crmBanner = new CrmBanner();
        BeanUtils.copyProperties(bannerVo,crmBanner);
        bannerService.updateById(crmBanner);
        return Result.success("修改成功");
    }

    /**
     * 删除图片
     * @param bannerId
     * @return
     */
    @PostMapping("{id}")
    public Result updateBanner(@PathVariable("id") String bannerId){
        bannerService.removeById(bannerId);
        return Result.success("删除成功");
    }

}